<template>
  <div class="follow">
    <div class="top">
      <!-- 顶部导航栏 -->
      <van-nav-bar>
        <template #title>
          关注/粉丝
        </template>
        <template #left>
          <div class="myicon" @click="back"><van-icon name="arrow-left" /></div
        ></template>
      </van-nav-bar>
    </div>
    <!-- 分页展示 -->
    <div class="showfollow">
      <van-tabs v-model="active" color="#3296fa">
        <van-tab title="关注">
          <div class="list" v-for="(val, index) in follow" :key="index">
            <div class="left">
              <div class="picture" @click="topeople(val.id)">
                <img :src="val.photo" alt="" />
              </div>
              <div class="txt">
                <div class="name">{{ val.name }}</div>
                <div class="fans">粉丝数{{ val.fans_count }}</div>
              </div>
            </div>
            <div class="right" :class="{ active: !val.isfollow }">
              <div v-if="val.isfollow" @click="changeState(val.id, index)">
                已关注
              </div>
              <div v-else @click="changeState(val.id, index)">
                未关注
              </div>
            </div>
          </div>
        </van-tab>
        <van-tab title="粉丝"><div class="nomore">没有更多了哦</div></van-tab>
      </van-tabs>
    </div>
  </div>
</template>

<script>
import {
  attention,
  Cancelattention,
  getUserFollowList
} from '../../../api/user'
export default {
  data () {
    return {
      active: '',
      // 关注用户信息
      follow: []
    }
  },
  methods: {
    back () {
      this.$router.back()
    },
    // 跳转到关注着的页面
    topeople (id) {
      this.$router.push(`/people?id=${id}`)
    },
    async changeState (id, index) {
      const list = [...this.follow]
      list.forEach(val => {
        if (val.id === id) {
          val.isfollow = !val.isfollow
        }
      })
      this.follow = list

      if (this.follow[index].isfollow) {
        const res1 = await attention({ target: id })
        console.log(res1)
      } else {
        const res = await Cancelattention(id)
        console.log(res)
      }
    }
  },
  async created () {
    const res = await getUserFollowList()
    console.log(res)
    res.data.data.results.forEach(val => {
      val.isfollow = true
    })
    this.follow = res.data.data.results

    this.active = +this.$route.params.active
  }
}
</script>

<style lang="less" scoped>
.follow {
  .top {
    /deep/ .van-nav-bar__content {
      background-color: #3296fa;
    }
    /deep/ .van-nav-bar__title.van-ellipsis {
      color: #fff;
    }
    .myicon {
      /deep/ i.van-icon.van-icon-arrow-left {
        color: #fff;
        font-size: 26px;
      }
    }
  }
  .showfollow {
    .list {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20px 15px 20px;
      border-bottom: 1px solid #999;
      font-size: 16px;
      .left {
        display: flex;
        align-items: center;
        .picture {
          img {
            width: 50px;
            height: 50px;
            border-radius: 50%;
          }
        }
        .txt {
          margin-left: 10px;
          .name {
            font-size: 14px;
          }
          .fans {
            margin-top: 8px;
            font-size: 12px;
            color: #999999;
          }
        }
      }
      .right {
        padding: 10px;
        border: 1px solid #999999;
        font-size: 13px;
      }
      .active {
        border: 1px solid red;
      }
    }
    .nomore {
      margin-top: 10px;
      font-size: 12px;
      color: #c0b8b5;
      text-align: center;
    }
  }
}
</style>
